package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.service.impl.BaseService;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.enums.friendRequest.FriendRequestStatus;
import com.ruoyi.common.enums.user.UserSex;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.vo.FriendRelationshipVO;
import com.ruoyi.system.domain.vo.FriendRequestVO;
import com.ruoyi.system.domain.vo.SysUserVO;
import com.ruoyi.system.mapper.FriendRelationshipMapper;
import com.ruoyi.system.service.IFriendRelationshipService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.FriendRequestMapper;
import com.ruoyi.system.domain.FriendRequest;
import com.ruoyi.system.service.IFriendRequestService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 好友申请Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-17
 */
@Service("friendRequestService")
@Slf4j
public class FriendRequestServiceImpl extends BaseService<FriendRequestMapper, FriendRequest, FriendRequestVO> implements IFriendRequestService {

    @Autowired
    private FriendRequestMapper friendRequestMapper;

    @Autowired
    private IFriendRelationshipService friendRelationshipService;

    /**
     * 查询好友申请
     * 
     * @param requestId 好友申请主键
     * @return 好友申请
     */
    @Override
    public FriendRequest selectFriendRequestByRequestId(Long requestId)
    {
        return friendRequestMapper.selectFriendRequestByRequestId(requestId);
    }

    /**
     * 查询好友申请列表
     * 
     * @param friendRequest 好友申请
     * @return 好友申请
     */
    @Override
    public List<FriendRequest> selectFriendRequestList(FriendRequest friendRequest)
    {
        return friendRequestMapper.selectFriendRequestList(friendRequest);
    }

    /**
     * 新增好友申请
     * 
     * @param friendRequest 好友申请
     * @return 结果
     */
    @Override
    public int insertFriendRequest(FriendRequest friendRequest)
    {
        friendRequest.setCreateTime(DateUtils.getNowDate());
        return friendRequestMapper.insertFriendRequest(friendRequest);
    }

    /**
     * 修改好友申请
     * 
     * @param friendRequest 好友申请
     * @return 结果
     */
    @Override
    public int updateFriendRequest(FriendRequest friendRequest)
    {
        friendRequest.setUpdateTime(DateUtils.getNowDate());
        return friendRequestMapper.updateFriendRequest(friendRequest);
    }

    /**
     * 批量删除好友申请
     * 
     * @param requestIds 需要删除的好友申请主键
     * @return 结果
     */
    @Override
    public int deleteFriendRequestByRequestIds(Long[] requestIds)
    {
        return friendRequestMapper.deleteFriendRequestByRequestIds(requestIds);
    }

    /**
     * 删除好友申请信息
     * 
     * @param requestId 好友申请主键
     * @return 结果
     */
    @Override
    public int deleteFriendRequestByRequestId(Long requestId) {
        return friendRequestMapper.deleteFriendRequestByRequestId(requestId);
    }

    @Override
    public boolean addFriend(FriendRequestVO infoVO) {

        FriendRequestVO queryVO = new FriendRequestVO();
        queryVO.setReceiverId(infoVO.getReceiverId());
        queryVO.setSenderId(infoVO.getSenderId());

        // 生成申请单  记录
        FriendRequest friendRequest = new FriendRequest();
        friendRequest.setSenderId(infoVO.getSenderId());
        friendRequest.setReceiverId(infoVO.getReceiverId());
        friendRequest.setMessage(infoVO.getMessage());
        friendRequest.setStatus(FriendRequestStatus.WAIT_FOR_HANDLE.getCode());
        friendRequest.setRemark(infoVO.getRemark());
        setVOInfo(friendRequest);

        FriendRequestVO oneFriendRequest = this.baseMapper.getOneFriendRequest(queryVO);
        if(oneFriendRequest != null){
            friendRequest.setFriendGroup(oneFriendRequest.getFriendGroup());
        }else {
            // 生成32位UUID
            String friendGroup = IdUtils.simpleUUID();
            log.info("addFriend friendGroup:{}", friendGroup);
            friendRequest.setFriendGroup(friendGroup);
        }

        int insert = friendRequestMapper.insertFriendRequest(friendRequest);
        return insert == 1;
    }

    @Transactional
    @Override
    public boolean handleFriendRequest(FriendRequestVO infoVO) {

        if(infoVO.getRequestId() == null){
            log.error("requestId is null");
            return false;
        }

        FriendRequest friendRequest = selectFriendRequestByRequestId(infoVO.getRequestId());
        friendRequest.setHandleTime(new Date());
        friendRequest.setStatus(infoVO.getStatus());

        int i = updateFriendRequest(friendRequest);

        // 新增好友关系表
        boolean result = true;
        if(FriendRequestStatus.YES.getCode().equals(infoVO.getStatus())){
            result = friendRelationshipService.handleAgree(infoVO);
        }
        return i == 1 && result;
    }

    @Override
    public List<FriendRequestVO> selRequestList(FriendRequestVO infoVO) {

        // 发送人 是 自己
        // 接受人 是 自己  的记录都要查询出来
        if(infoVO.getCurUserId() == null){
            infoVO.setCurUserId(SecurityUtils.getUserId());
        }
        List<FriendRequestVO> requestVOList = this.baseMapper.selRequestList(infoVO);
        handleUserEnum(requestVOList);
        return requestVOList;
    }

    @Override
    public FriendRequestVO getOneFriendRequest(FriendRequestVO infoVO) {
        FriendRequestVO requestVO = this.baseMapper.getOneFriendRequest(infoVO);
        handleVO(requestVO);
        return requestVO;
    }

    @Override
    public List<FriendRequestVO> queryUserWithFriendShip(FriendRequestVO queryVO) {

        List<FriendRequestVO> result = this.baseMapper.queryUserWithFriendShip(queryVO);

        return result;
    }

    /**
     * 好友申请表枚举处理
     */
    public static void handleUserEnum(List<FriendRequestVO> friendRequestVOList) {
        for (FriendRequestVO friendRequestVO : friendRequestVOList){
            handleVO(friendRequestVO);
        }
    }

    private static void handleVO(FriendRequestVO friendRequestVO) {
        if(friendRequestVO != null){
            //处理状态
            if(friendRequestVO.getStatus() != null){
                if(friendRequestVO.getStatus().equals(FriendRequestStatus.WAIT_FOR_HANDLE.getCode())){
                    friendRequestVO.setStatusStr(FriendRequestStatus.WAIT_FOR_HANDLE.getInfo());
                }
                if(friendRequestVO.getStatus().equals(FriendRequestStatus.YES.getCode())){
                    friendRequestVO.setStatusStr(FriendRequestStatus.YES.getInfo());
                }
                if(friendRequestVO.getStatus().equals(FriendRequestStatus.REJECT.getCode())){
                    friendRequestVO.setStatusStr(FriendRequestStatus.REJECT.getInfo());
                }
            }
        }
    }


}
